import { ref, reactive } from "vue";
import { useDynamicStore } from "@/stores/modules/dynamic";
import { useDictionryStore } from "@/stores/modules/dictionary";
import { useCatalogStore } from "@/stores/modules/catalog";
import { useUserStore } from "@/stores/modules/user";
import { columnCoverter } from "@/views/system/dynamicMain/ts/until";
import ACrypto from "@/utils/crypto";
/**
 * @description 表格格式化
 * */
export const useFormatter = () => {
  const dynamicStore = useDynamicStore();
  const dictionryStore = useDictionryStore();
  const catalogStore = useCatalogStore();
  const catalogList = catalogStore.catalogList;
  let ruleList = dynamicStore.visibleConfig.FieldRule.filter((v: any) => v.FR_RuleType === "display");
  const formatter = (row, data) => {
    const cellValue = data[row.Field_Name];
    const tempObj = ruleList.find((v: any) => v.FR_TableField === row.Field_Name);
    let result = null;
    let param = null;
    if (tempObj) {
      // 字典转换
      param = tempObj.FR_Rule.split(":")[1];
      if (tempObj.FR_Rule.includes("dictionary:")) {
        result = columnCoverter(tempObj.FR_Rule, cellValue);
      } else if (tempObj.FR_Rule.toLowerCase().includes("datetime:")) {
        // 时间转换
        result = dtime(row[tempObj.FR_TableField]).format(param);
      } else if (tempObj.FR_Rule.toLowerCase().includes("userinfo:")) {
        // 人员的id转换
        const temp = userStore.getUserList.find((v: any) => v.U_ID === cellValue);
        result = temp ? temp[param] : "";
      } else if (tempObj.FR_Rule.toLowerCase().includes("catalogconvert:")) {
        // 分类id转换
        const temp = catalogList.find((v: any) => v.Catalog_ID && v.Catalog_ID === cellValue);
        result = temp ? temp[param] : "";
      } else if (tempObj.FR_Rule.toLowerCase().includes("orgconvert:")) {
        const temp = catalogList.find(v => v[tempObj.Field_Name] === cellValue);
        result = temp ? temp[param] : "";
      } else if (tempObj.FR_Rule.toLowerCase().includes("jm")) {
        // 对加密字段转换
        result = ACrypto.getSS(row[tempObj.FR_TableField]);
      } else if (tempObj.FR_Rule.toLowerCase().includes("tozh")) {
        if (row[tempObj.FR_TableField]) {
          result = "是";
        } else {
          result = "否";
        }
      }
    }
    if (result) {
      return result;
    }
    return cellValue;
  };
  return {
    formatter
  };
};
